package com.newbee.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.hhu.newbee.R;
import com.newbee.adapter.ConferenceFriendsListvewAdapter;
import com.newbee.app.OnRefrushActivity;
import com.newbee.listbean.FriendsInfoBean;
import com.newbee.service.MainService;
import com.newbee.service.MainServiceTask;
import com.newbee.service.TaskType;
import com.newbee.smack.SmackClient;
import com.newbee.util.MD5Util;

public class NewSendToFriendsActivity extends Activity implements
		OnRefrushActivity, OnClickListener, OnItemClickListener
{
	public static final String EMAIL_CONTET = "EMAIL_CONTET";

	private ListView mListView;

	private View mBack;
	private List<FriendsInfoBean> mDataList;

	private ImageButton mCreate;

	private ConferenceFriendsListvewAdapter mAdapter;
	private List<String> mSelectList = new ArrayList<String>();

	private EditText mUserMail;
	private SmackClient msc;

	private TextView mEmptyview;

	private EditText mPassWord;

	private String mContentStr;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.fragement_send_mail_friendslist);
		mEmptyview = (TextView) findViewById(R.id.send_empty);
		mListView = (ListView) findViewById(R.id.sendmail_listview);
		initLayout();
		initDatas();

	}

	private void initDatas()
	{
		MainServiceTask mst = new MainServiceTask(TaskType.GET_FRIEND, null,
				this.getClass().getName());
		MainService.addActivityMap(this.getClass().getName(), this);
		MainService.addTaskQueue(mst);

		Bundle bundle = this.getIntent().getExtras();

		mContentStr = bundle.getString(EMAIL_CONTET, "内容发生错误，请重试");

	}

	private void initLayout()
	{
		mListView = (ListView) findViewById(R.id.sendmail_listview);
		mListView.setOnItemClickListener(this);
		mBack = (View) findViewById(R.id.mailsend_friend_choose_back);
		mBack.setOnClickListener(this);
		mCreate = (ImageButton) findViewById(R.id.conference_friends_select_sendmail);
		mCreate.setOnClickListener(this);
	}

	@Override
	public void init()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public RtInfo refresh(int taskID, Object... paras)
	{
		switch (taskID)
		{
		case TaskType.GET_FRIEND:
		{

			this.mDataList = (List<FriendsInfoBean>) paras[0];

			if (this.mDataList.size() == 0)
			{
				this.mListView.setEmptyView(mEmptyview);
			}
			else if (mAdapter == null)
			{

				mAdapter = new ConferenceFriendsListvewAdapter(mDataList, this);
				this.mListView.setAdapter(mAdapter);

			}
			else
			{
				mAdapter.setList(mDataList);
				mAdapter.notifyDataSetChanged();
			}

			break;
		}
		case TaskType.CREATE_MULT_CHAT:
		{

			boolean j = (Boolean) paras[0];
			ArrayList<String> users = (ArrayList<String>) paras[2];// 带有后缀的用户名列表
			if (j)
			{

			}
			else
			{
				Toast.makeText(this, "创建协同录音失败！", Toast.LENGTH_LONG).show();
			}

			break;
		}

		default:
			break;
		}
		return null;

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3)
	{

		CheckBox checkBox = (CheckBox) v.findViewById(R.id.conference_checkbox);

		System.out.println("click---------------------->" + position);
		FriendsInfoBean fib = mDataList.get(position);

		if (checkBox.isChecked())
		{
			checkBox.setChecked(false);
			mSelectList.remove(fib.getEmail());
		}
		else
		{
			checkBox.setChecked(true);
			mSelectList.add(fib.getEmail());
		}

	}

	@Override
	public void onDestroy()
	{
		MainService.removeActivity(this.getClass().getName());
		super.onDestroy();
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.conference_friend_choose_back:
		{
			this.finish();
			break;
		}
		case R.id.conference_friends_select_sendmail:
		{
			int length = mSelectList.size();
			if (length <= 0)
			{
				Toast.makeText(this, "选择用户不能为空！", Toast.LENGTH_LONG).show();
				return;
			}
			SharedPreferences sp = this.getSharedPreferences(
					LoginActivity.SP_USER_INFO, Activity.MODE_MULTI_PROCESS);
			final String nickName = sp.getString(LoginActivity.SP_KEY_NICKNAME,
					"");
			Intent intent = new Intent(Intent.ACTION_SENDTO);

			intent.setData(Uri.parse("mailto:"
					+ mSelectList.get(0).split("\\$")[0].replace("#", "@")));

			String[] exUsers = new String[length - 1];

			for (int i = 1; i < length; i++)
			{
				exUsers[i - 1] = mSelectList.get(i).split("\\$")[0].replace(
						"#", "@");
			}
			intent.putExtra(Intent.EXTRA_EMAIL, exUsers);

			String subject = "来自" + nickName + " " + MD5Util.getNowTime()
					+ "的录音转换文本 by 语音小助手";

			intent.putExtra(Intent.EXTRA_SUBJECT, subject);

			// MultChatActivity mca = (MultChatActivity)
			// MultChatSendMailChooseFriendFragement.this
			// .getActivity();

			intent.putExtra(Intent.EXTRA_TEXT, mContentStr);// 这个地方获取要发送的文本；
			startActivity(intent);
			break;
		}
		case R.id.mailsend_friend_choose_back:
		{
			break;
		}
		default:
			break;
		}

	}
}